local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local FunctionList = ClientOHOH.load.FunctionList();
local ui_list=UIManager().ui_list
local GlobalTipGui=ui_list.GlobalTipGui
local Buff_Cfg=ClientOHOH.config.Buff
local Mall_Cfg=ClientOHOH.config.Mall
local LanguageHelper=_G.LanguageHelper 
local gamePass_cfg = ClientOHOH.config.GamePass;
local gUserDataEvent=nil
local ShopGui=UIManager:CreateUI("ShopGui",require(script.ShopGui))
local diamondGui = ShopGui:Get("diamond")
local gamepassGui = ShopGui:Get("gamepass")
local potionsGui = ShopGui:Get("potions")
local ConnectClass = _G.CommonOHOH.load.Connect()
local Good_Cfg=ClientOHOH.config.Goods
local Diamond_Cfg = {}
local Gamepass_Cfg = {}
local Potions_Cfg = {}
local dataStore = nil
local ItemTool = _G.CommonOHOH.load.ItemTool()
local BigNumberHelper = ClientOHOH.load.BigNumberHelper()

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent

    dataStore = self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent())
    gUserDataEvent=self.parent.UserDataEvent
    --购买事件触发函数
    

    M:AddMallCfg()
    M.Init()
    self:AddConnect()
end;

--填充ui
function M.Init()
    table.sort(Diamond_Cfg,function(a,b)
        return a.id<b.id
    end)

    table.sort(Gamepass_Cfg,function(a,b)
        return a.id<b.id
    end)
    table.sort(Potions_Cfg,function(a,b)
        return a.id<b.id
    end)


    diamondGui.Full(Diamond_Cfg)
    gamepassGui.Full(Gamepass_Cfg)
    potionsGui.Full(Potions_Cfg)
end

function M.AddConnect(self)
    ShopGui.ShopBuyEvent:Connect(function(data)
        self.player.RobuxBuyEvent:Action(data.gid)
    end)

    --兑换码
    ShopGui.CodeExchangeEvent:Connect(function(code)
        self:CodeExchange(code)
    end)
    ------------gamepass------------
	gUserDataEvent:Connect(function(data)
        if data["gamePass"] then
            for name, v in pairs(data["gamePass"]) do
                for _, value in pairs(Gamepass_Cfg) do
                    if value.name == name then
                        value.isHad = true
                        break
                    end
                end
            end
            for _, value in pairs(Gamepass_Cfg) do
                if value["category"] then
                    value:SetHad(data["gamePass"])
                end
            end
            gamepassGui.UpdateGamePass(data["gamePass"])
        end
    end)
    
end

--生成配置数据
function M.AddMallCfg(self)
    for _, value in pairs(Mall_Cfg) do
        if value.belong == "gamepass" then
            self:AddGamePassCfg(value)
        else if value.belong == "diamond" then
            self:AddDiamondCfg(value)
        elseif value.belong == "potions" then
            self:AddPotionsCfg(value)
        end    
        end
    end
end


--以下AddCfg函数传入的data都为单条Mall中的数据
function M.AddGamePassCfg(self,data)
    assert(Good_Cfg[data.gid],"不存在计费点:"..data.gid)
    data["price"] = Good_Cfg[data.gid].price
    data["buyInfo"] = {
        gid = Good_Cfg[data.gid].id,
        gType = Good_Cfg[data.gid].gType
    }
    data["goodData"] = Good_Cfg[data.gid]
    local gamePassData = dataStore:GetData("gamePass")
    local value = nil
    for k, v in pairs(Good_Cfg[data.gid].itemData) do
        data["name"] = k
    end
    for index, _ in pairs(Good_Cfg[data.gid].itemData) do
        if gamePassData[index] then
            data["isHad"] = true
        else
            data["isHad"] = false
        end
    end
    table.insert(Gamepass_Cfg,data)
end

function M.AddDiamondCfg(self,data)
    assert(Good_Cfg[data.gid],"不存在计费点:"..data.gid)
    data["price"] = Good_Cfg[data.gid].price
    for _, value in pairs(Good_Cfg[data.gid].itemData) do
        data["num"] = "+"..BigNumberHelper:ToString(value)
    end
    --用于购买的真正gid和type
    data["buyInfo"] = {
        gid = Good_Cfg[data.gid].id,
        gType = Good_Cfg[data.gid].gType
    }
    data["goodData"] = Good_Cfg[data.gid]
    
    table.insert(Diamond_Cfg,data)
end

function M.AddPotionsCfg(self,data)
    assert(Good_Cfg[data.gid],"不存在计费点:"..data.gid)
    local buy1Gid = data.arg[1]
    local buy10Gid = data.arg[2]
    data["buy1"] = {}
    data["buy10"] = {}
    data["buy1"]["price"] = Good_Cfg[buy1Gid].price
    data["buy10"]["price"] = Good_Cfg[buy10Gid].price

    data["buy1"]["buyInfo"] = {
        gid = Good_Cfg[buy1Gid].id,
        gType = Good_Cfg[buy1Gid].gType
    }
    data["buy10"]["buyInfo"] = {
        gid = Good_Cfg[buy10Gid].id,
        gType = Good_Cfg[buy10Gid].gType
    }
    data["goodData"] = Good_Cfg[buy1Gid]
    for pType, value in pairs(Good_Cfg[buy1Gid].itemData) do
        data["buy1"]["sellCount"] = value
        data["itemId"] = pType
        local list = string.split(pType,"_")
        --buffId :20000
        data["buffId"] = list[#list]
    end
    for _, value in pairs(Good_Cfg[buy10Gid].itemData) do
        data["buy10"]["sellCount"] = value
    end
    data["buffInfo"]= Buff_Cfg[tonumber(data["buffId"])]
    table.insert(Potions_Cfg,data)
end


function M.CodeExchange(self,code)
	if not code then
		return
	end
	--去除回车换行
	code = string.gsub(code, "\r\n", "")
	--去除空格
	code = string.gsub(code, "%s+", "")
	--转为大写
	code = string.upper(code)

	if code =="" then
		return
	end
	local reward=ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.code_exchange,code)
	if reward and #reward ~= 0 then
		self:AddProcess(function()
			for k,v in pairs(reward) do
				local image,isImage=_G.CommonOHOH.load.ItemTool().GetItemShowView(k)
				if image and isImage then
					ClientOHOH.load.UIManager().ui_list.GlobalTipGui:ShowTextAndImage("+"..v,image)
					self:Wait(0.5)
				end
			end
		end)
		ClientOHOH.load.Client_AudioManager():PlayGloble("claim")
	else
		ClientOHOH.load.UIManager().ui_list.GlobalTipGui:Show(LanguageHelper:GetText(22) )
	end
end




function M.dtor(self)
	M.super.dtor(self);
end;

return M